package com.yao.mybatis.config;

import com.yao.mybatis.utils.DocumentUtils;
import org.dom4j.Element;

import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
 * 解析mapper映射文件
 * @author Administrator
 */
public class XMLMapperParser {

    private Configuration configuration;

    public XMLMapperParser(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析每个mapper.xml文件
     * @param inputStream
     * @return
     */
    public void parse(InputStream inputStream){
        Element rootElement = Objects.requireNonNull(DocumentUtils.readFileAsDocument(inputStream)).getRootElement();
        String namespace = rootElement.attributeValue("namespace");
        //解析select标签
        List<Element> select = rootElement.elements("select");
        XMLStatementParser xmlStatementParser = new XMLStatementParser(configuration);
        for (Element e:select){
            xmlStatementParser.parse(e,namespace);
        }
    }
}

